﻿@model ProductModel
@inject AdminAreaSettings adminAreaSettings
<div class="note note-info">
    <p>
        <strong>@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Description")</strong>
    </p>
    <p>
        @T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Description2")
    </p>
</div>
<div class="panel panel-default">
    <vc:admin-widget widget-zone="product_details_product_attributes_combinations_top" additional-data="Model" />
    <div class="panel-body">
        <div id="attributecombinations-grid"></div>
    </div>
    <div class="panel-footer">
        <button type="submit" id="btnAddNewCombination" onclick="javascript:OpenWindow('@(Url.Action("AttributeCombinationPopup", "Product", new { productId = Model.Id, btnId = "btnRefreshCombinations", formId = "product-form" }))', 800, 600, true); return false;" class="k-button"><i class="fa fa-plus"></i>&nbsp;@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.AddNew")</button>
        <button type="submit" id="btnGenerateAllCombinations" class="k-button"><span class="k-icon k-i-style-builder"></span>&nbsp;@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.GenerateAll")</button>
        <button type="submit" id="btnClearAllCombinations" class="k-button"><span class="k-icon k-i-cut"></span>&nbsp;@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.ClearAll")</button>
        <input type="submit" id="btnRefreshCombinations" style="display: none" />
    </div>
    <vc:admin-widget widget-zone="product_details_product_attributes_combinations_bottom" additional-data="Model" />
</div>

<script>
    $(document).ready(function() {
        $("#attributecombinations-grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("ProductAttributeCombinationList", "Product", new { productId = Model.Id }))",
                        type: "POST",
                        dataType: "json",
                        data: addAntiForgeryToken
                    },
                    update: {
                        url: "@Html.Raw(Url.Action("ProductAttributeCombinationUpdate", "Product"))",
                        type: "POST",
                        dataType: "json",
                        data: addAntiForgeryToken
                    },
                    destroy: {
                        url: "@Html.Raw(Url.Action("ProductAttributeCombinationDelete", "Product"))",
                        type: "POST",
                        dataType: "json",
                        data: addAntiForgeryToken
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors",
                    model: {
                        id: "Id",
                        fields: {
                            AttributesXml: { editable: false, type: "string" },
                            Warnings: { editable: false, type: "string" },
                            StockQuantity: { editable: true, type: "number" },
                            AllowOutOfStockOrders: { editable: true, type: "boolean" },
                            Sku: { editable: true, type: "string" },
                            ManufacturerPartNumber: { editable: true, type: "string" },
                            Gtin: { editable: true, type: "string" },
                            OverriddenPrice: { editable: true, type: "number" },
                            NotifyAdminForQuantityBelow: { editable: true, type: "number" },
                            Id: { editable: false, type: "string" }
                        }
                    }
                },
                requestEnd: function(e) {
                    if (e.type == "update") {
                        this.read();
                    }
                },
                error: function(e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                pageSize: @(adminAreaSettings.DefaultGridPageSize),
            },
            pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
            editable: {
                confirmation: true,
                mode: "inline"
            },
            scrollable: false,
            columns: [
                {
                    field: "AttributesXml",
                    title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.Attributes")",
                    width: 340,
                    //template: "#=AttributesXml#<br /> # if(Warnings !== null) {# <span style=\"color: red\">#=Warnings#</span> #}  #",
                    encoded: false,
                    template: "<a class='k-link' href='javascript:OpenWindow(\"@Url.Content("~/Admin/Product/AttributeCombinationPopup/")?productId=@(Model.Id)&Id=#=Id#&btnId=btnRefreshCombinations&formId=product-form\", 800, 600, true);'>#=AttributesXml#<br /> # if(Warnings !== null) {# <span style=\"color: red\">#=Warnings#</span> #}  #</a>"
                }, {
                    field: "StockQuantity",
                    title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.StockQuantity")",
                    width: 80,
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    //integer format
                    format: "{0:0}"
                }, {
                    field: "AllowOutOfStockOrders",
                    title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.AllowOutOfStockOrders")",
                    width: 80,
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    template: '# if(AllowOutOfStockOrders) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                }, {
                    field: "Sku",
                    title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.Sku")",
                    width: 100
                }, {
                    field: "ManufacturerPartNumber",
                    title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.ManufacturerPartNumber")",
                    width: 100
                }, {
                    field: "Gtin",
                    title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.Gtin")",
                    width: 100
                }, {
                    field: "OverriddenPrice",
                    title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.OverriddenPrice")",
                    width: 80,
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    editor: function (container, options) {
                        $('<input name="' + options.field + '"/>')
                                .appendTo(container)
                                .kendoNumericTextBox({
                                    format: "{0:n4}",
                                    decimals: 4
                                });
                    }
                },
                {
                    command: [
                        {
                            name: "destroy",
                            text: "@T("Admin.Common.Delete")"
                        }
                    ],
                    width: 50
                }
            ]
        });
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        //refresh button
        $('#btnRefreshCombinations').click(function () {
            //refresh grid
            var grid = $("#attributecombinations-grid").data('kendoGrid');
            grid.dataSource.read();

            //return false to don't reload a page
            return false;
        });


        //generate combinations
        $('#btnGenerateAllCombinations').click(function () {
            if (confirm('@T("Admin.Common.AreYouSure")')) {

                var postData = {
                };
                addAntiForgeryToken(postData);

                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.Action("GenerateAllAttributeCombinations", "Product", new { productId = Model.Id })',
                    data: postData,
                    dataType: 'json',
                    success: function(data) {
                        //refresh grid
                        var grid = $("#attributecombinations-grid").data('kendoGrid');
                        grid.dataSource.read();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert('Error while generating attribute combinations');
                    }
                });
            }
            return false;
        });

        //clear combinations
        $('#btnClearAllCombinations').click(function () {
            if (confirm('@T("Admin.Common.AreYouSure")')) {

                var postData = {
                };
                addAntiForgeryToken(postData);

                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.Action("ClearAllAttributeCombinations", "Product", new { productId = Model.Id })',
                    data: postData,
                    dataType: 'json',
                    success: function(data) {
                        //refresh grid
                        var grid = $("#attributecombinations-grid").data('kendoGrid');
                        grid.dataSource.read();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert('Error while clearing attribute combinations');
                    }
                });
            }
            return false;
        });

    });
</script>
